$(function(){
    var obj = { date: new Date(), year: -1, month: -1, priceArr: [] , day: "" , lastDay: 0};
    var htmlObj = { header: "", left: "",right: "" };
    var expire = new Date(2017,10,19);
    var expireYear = expire.getFullYear();
    var expireMonth = expire.getMonth() + 1;
    var expireDate = expire.getDate();
    var pickerEvent = {
        Init: function(){
            //设置日历html;
            //初始化数据;
            dateUntil.getCurrent();
            //开始时间和截止时间相差几个月;
            var yIndex = expireYear - obj.year;
            var mIndex = (yIndex * 12) + expireMonth - obj.month;
            obj.month = obj.month-1;
            for(var i = 0; i <= mIndex; i++){
                //设置起始日期;因第一个月的起始时间不一样;
                var day = i > 0 ? 1 : obj.day;
                //截止日，计算有几周;到截止日有几周,好设置行数;
                obj.lastDay = i == mIndex ? expireDate : 0;
                //重新定义时间;
                obj.date = new Date(obj.year, obj.month ,day);
                dateUntil.getCurrent();
                for (var item in pickerHtml) {
                    pickerHtml[item]();
                }
                var html = '<div id="calendar_choose" class="mod-calendar">';
                html += htmlObj.left;
                html += htmlObj.header;
                html += '<div id="bigCalendar">';
                html += htmlObj.right;
                html += "</div></div>";
                $(document.body).append(html);
            }         
        },
        setPriceArr: function(arr) {
        	obj.priceArr = arr;
        }
    }
    var pickerHtml = {
        getHead: function(){
            var head = '<ul class="calendar-num"><li class="bold">日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li class="bold">六</li></ul>';
            htmlObj.header = head;
        },
        getLfet: function(){
            var left = '<div class="calendar-top"><p class="date_text">' + obj.year + '年' + obj.month + '月</p></div>';
            htmlObj.left = left;
        },
        getRight: function(){
            var html = '<table id="calendar_tab" class="calendar-content"><tbody>';
            //行数;
            var index = dateUntil.getMonthWeek();
            //最后一天;
            var days = obj.lastDay == 0 ? dateUntil.getLastDay() : obj.lastDay;
            var week = dateUntil.getWeek();
            //行数;
            for (var i = 0; i < index; i++) {
                html += "<tr>";
                //列数;
                for(var k = 0; k <= 6; k++) {
                    //7*i+k对应每一个小表格;
                    var idx = 7 * i + k + obj.day -week; 
                    //显示 
                    var date_text = obj.year + '-' + obj.month + '-' + idx;
                    var priceArr = obj.priceArr[date_text];
                    var price_date_string = '';
                    var lis = '';
                    if (priceArr) {
                    	price = priceArr.price;
                    	price_date_string = 'data-price="'+price+'"';
                    	lis += '<a class="price" href="javascript:;">'+price+'元'+'</a>';
                    }else {
                    	lis += '<a class="add_date_price J_add_date_price" href="javascript:;">添加</a>'
                    }
                    
                    if(idx <= days && idx > 0 ) {
                        if(idx >= obj.day) {
                            html += '<td  data-date="' + obj.year + "-" + obj.month + "-" + idx +'"' + price_date_string +'>' + '<div class="li_wrap"><p>' + idx + '</p>' + lis + '</div></td>';
                        }else {
                            html += '<td class="disable">' + '<div class="li_wrap"><p>' + idx + '</p>' + '</td>';
                        }   
                    }else {
                        html += '<td class="disable"></td>';
                    }
                }
                html += '</tr>';
            }
            html += "</tbody></table>";
            htmlObj.right = html;
        }
    };
    var dateUntil = {
        //根据日期得到星期
        getWeek: function(){
            var d = new Date(obj.year, obj.month-1, obj.day);
            return d.getDay();
        },
        //得到一个月的天数
        getLastDay: function(){
            var new_year = obj.year;//取当前的年份        
            var new_month = obj.month;//取下一个月的第一天，方便计算（最后一不固定）        
            var new_date = new Date(new_year, new_month, 1);                //取当年当月中的第一天      
            return (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate();//获取当月天数
        },
        //得到一个月有几周
        getMonthWeek: function(){
        	var month = obj.lastDay == 0 ? obj.month : obj.month-1;
            var d = new Date(obj.year, month, obj.lastDay),
                w = obj.date.getDay(),
                f = d.getDate();
            return Math.ceil(
                    (f - obj.day + 1 + w) / 7
                );
        },
        //获取年月日;
        getCurrent: function(){
            var dt = obj.date;
            obj.year = dt.getFullYear();
            obj.month = dt.getMonth() + 1;
            obj.day = dt.getDate();
        }
    };
    pickerEvent.setPriceArr(ticket_price_json);
    pickerEvent.Init();

})
